﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using Models;
using BLL;
using System.Net.Mail;
using System.Net;

namespace ShiCaiWang1
{
    public partial class C_User : System.Web.UI.Page
    {
        private UserBLL ub = new UserBLL();
        private ProjectBLL pb = new ProjectBLL();
        private UserInfo user = new UserInfo();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("Collection");
                if (dt.Rows.Count == 0)
                {
                    Inis();
                    Inib();
                }
            }
        }
        private void Inib()
        {
            //this.UserPhoto. = Session["User_HeadPic"].ToString();
            this.Emailtxt.Text = Session["Email"].ToString();
            this.EamilText.Text = Session["Email"].ToString();
            this.Phonetxt.Text = Session["Phone"].ToString();
            this.Phonetxt2.Text = Session["Phone"].ToString();
            this.PhotoText.Text = Session["Phone"].ToString();
            this.Label1.Text = Session["Phone"].ToString();
            this.Phototxt.Text = Session["Phone"].ToString();
            this.UserName.Text = Session["UserName"].ToString();
            //this.Phonetxt.Text = Session["username"].ToString();
            //this.Citytxt.Text = Session["username"].ToString();
            Inis();
        }
        private void Inis()
        {
            int i = int.Parse(Session["UserID"].ToString());
            //条件查询
            DataSet ds = pb.GetProjectList(i);
            this.Repeater1.DataSource = ds;
            this.Repeater1.DataBind();
        }
        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)

        {

            if (e.CommandName == "del") //删除操作
            {
                //string id = e.CommandArgument.ToString();
                //int i = UserBLL.Delete(id);
                //if (i < 1)
                //{
                //    Response.Write("<script>alert('操作失败')</script>");
                //    return;
                //}
                //Inis();
                //Response.Write("<script>alert('操作成功')</script>");

                //string que = "delete from [gz_gzsrb] where id='" + e.CommandArgument.ToString().Trim() + "'";
                //DBSqlHelp.ExecuteSql(3, que);//自己定义的一个DBSqlHelp类，执行sql语句]

            }

        }

        //修改用户信息
        protected void btn_Click(object sender, EventArgs e)
        {
            user.UserID= int.Parse(Session["UserID"].ToString());
            user.Name = UserName.Text.ToString().Trim();
            //user.Email= EamilText.Text.ToString().Trim();
            if (ub.UpUser(user) == false)
            {
                Response.Write("<script>alert('修改失败')</script>");
                return;
            }
            Response.Write("<script>alert('修改成功')</script>");
        }
        //修改密码
        protected void btn2_Click(object sender, EventArgs e)
        {

        }
        //重新绑定手机
        protected void btn3_Click(object sender, EventArgs e)
        {

        }

        protected void Emailbtn_Click(object sender, EventArgs e)
        {
            if (TextBox5.Text == "")
            {
                EmailLabel.Text = "请输入验证码";
                return;
            }
            else
            {
                //实例化一个发送邮件类。
                MailMessage mailMessage = new MailMessage();
                //发件人邮箱地址，方法重载不同，可以根据需求自行选择。
                mailMessage.From = new MailAddress("397076115@qq.com");
                //收件人邮箱地址。
                mailMessage.To.Add(new MailAddress(this.Emailtxt.Text));
                //邮件标题。
                mailMessage.Subject = "这是你的验证码";
                string verificationcode = createrandom(6);
                user.EmailCode = verificationcode.ToString();
                //邮件内容。
                mailMessage.Body = "你的验证码是" + verificationcode;
                //实例化一个SmtpClient类。
                SmtpClient client = new SmtpClient();
                //在这里我使用的是qq邮箱，所以是smtp.qq.com，如果你使用的是126邮箱，那么就是smtp.126.com。
                client.Host = "smtp.qq.com";
                //使用安全加密连接。
                client.EnableSsl = true;
                //不和请求一块发送。
                client.UseDefaultCredentials = false;
                //验证发件人身份(发件人的邮箱，邮箱里的生成授权码);
                client.Credentials = new NetworkCredential("397076115@qq.com", "mpfelfsyxgaobgjc");
                //发送
                client.Send(mailMessage);
                //MessageBox.Show("发送成功！", "恭喜！");
                Response.Write("<script>alert('发送成功')</script>");
            }
        }

        private string createrandom(int codelengh)
        {
            int rep = 0;
            string str = string.Empty;
            long num2 = DateTime.Now.Ticks + rep;
            rep++;
            Random random = new Random(((int)(((ulong)num2) & 0xffffffffL)) | ((int)(num2 >> rep)));
            for (int i = 0; i < codelengh; i++)
            {
                char ch;
                int num = random.Next();
                if ((num % 2) == 0)
                {
                    ch = (char)(0x30 + ((ushort)(num % 10)));
                }
                else
                {
                    ch = (char)(0x41 + ((ushort)(num % 0x1a)));
                }
                str = str + ch.ToString();
            }
            return str;

        }
    }
}